package com.cat.slidingWindow;

/**
 * @author cat
 * @description https://leetcode.cn/problems/count-number-of-nice-subarrays/description/
 * @create 2025/7/31 09:59
 * @since JDK17
 */

public class Solution18 {
    public int numberOfSubarrays(int[] nums, int k) {
        int ans = 0, cnt = 0, n = nums.length, j;
        for (int l = 0, r = 0; r < n; ) {
            while (r < n && cnt < k) {  // 求到k个奇数
                cnt += nums[r++] % 2 == 1 ? 1 : 0;
            }
            j = r + 1;
            while (r < n && nums[r] % 2 == 0) { //
                r++;
            }
            while (cnt >= k) {
                cnt -= nums[l++] % 2 == 1 ? 1 : 0;
                ans += r - j;
            }
        }
        return ans;
    }
}
